<template>
  <el-dialog
    :title="!dataForm.liveId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="直播名" prop="liveName">
      <el-input v-model="dataForm.liveName" placeholder="直播名"></el-input>
    </el-form-item>
    <el-form-item label="开始时间" prop="startDate">
      <el-input v-model="dataForm.startDate" placeholder="开始时间"></el-input>
    </el-form-item>
    <el-form-item label="结束时间" prop="endDate">
      <el-input v-model="dataForm.endDate" placeholder="结束时间"></el-input>
    </el-form-item>
    <el-form-item label="拉流" prop="upUrl">
      <el-input v-model="dataForm.upUrl" placeholder="拉流"></el-input>
    </el-form-item>
    <el-form-item label="推流" prop="downUrl">
      <el-input v-model="dataForm.downUrl" placeholder="推流"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          liveId: 0,
          liveName: '',
          startDate: '',
          endDate: '',
          upUrl: '',
          downUrl: ''
        },
        dataRule: {
          liveName: [
            { required: true, message: '直播名不能为空', trigger: 'blur' }
          ],
          startDate: [
            { required: true, message: '开始时间不能为空', trigger: 'blur' }
          ],
          endDate: [
            { required: true, message: '结束时间不能为空', trigger: 'blur' }
          ],
          upUrl: [
            { required: true, message: '拉流不能为空', trigger: 'blur' }
          ],
          downUrl: [
            { required: true, message: '推流不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.liveId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.liveId) {
            this.$http({
              url: this.$http.adornUrl(`/live/livestream/info/${this.dataForm.liveId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.liveName = data.liveStream.liveName
                this.dataForm.startDate = data.liveStream.startDate
                this.dataForm.endDate = data.liveStream.endDate
                this.dataForm.upUrl = data.liveStream.upUrl
                this.dataForm.downUrl = data.liveStream.downUrl
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/live/livestream/${!this.dataForm.liveId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'liveId': this.dataForm.liveId || undefined,
                'liveName': this.dataForm.liveName,
                'startDate': this.dataForm.startDate,
                'endDate': this.dataForm.endDate,
                'upUrl': this.dataForm.upUrl,
                'downUrl': this.dataForm.downUrl
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
